<template><div><h2 id="正则表达式各字符的意义" tabindex="-1"><a class="header-anchor" href="#正则表达式各字符的意义"><span>正则表达式各字符的意义</span></a></h2>
<ul>
<li>[]  允许的字符</li>
<li>{} 字符的个数</li>
<li>^ 以谁开头</li>
<li>$ 以谁结尾</li>
<li>一些特殊字符如. 要用转义</li>
</ul>
<h2 id="常用正则表达式" tabindex="-1"><a class="header-anchor" href="#常用正则表达式"><span>常用正则表达式</span></a></h2>
<div class="language-java line-numbers-mode has-collapsed-lines collapsed" data-highlighter="shiki" data-ext="java" style="--vp-collapsed-lines:15;background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#7F848E;font-style:italic">// 姓名 2-4个中文字符正则</span></span>
<span class="line"><span style="color:#56B6C2">/^</span><span style="color:#E06C75">[\u4e00</span><span style="color:#56B6C2">-</span><span style="color:#E06C75">\u9fa5]{</span><span style="color:#D19A66">2</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66">4</span><span style="color:#E06C75">}$</span><span style="color:#56B6C2">/</span><span style="color:#E06C75">    </span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">// 手机号码</span></span>
<span class="line"><span style="color:#56B6C2">/^</span><span style="color:#E06C75">(</span><span style="color:#D19A66">13</span><span style="color:#E06C75">[</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">]</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">14</span><span style="color:#E06C75">[01456879]</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">15</span><span style="color:#E06C75">[</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">35</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">]</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">16</span><span style="color:#E06C75">[</span><span style="color:#D19A66">2567</span><span style="color:#E06C75">]</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">17</span><span style="color:#E06C75">[</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">8</span><span style="color:#E06C75">]</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">18</span><span style="color:#E06C75">[</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">]</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">19</span><span style="color:#E06C75">[</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">35</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">])\d{</span><span style="color:#D19A66">8</span><span style="color:#E06C75">}$</span><span style="color:#56B6C2">/</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">// 座机号码</span></span>
<span class="line"><span style="color:#56B6C2">/^</span><span style="color:#E06C75">(</span><span style="color:#D19A66">0</span><span style="color:#E06C75">\d{</span><span style="color:#D19A66">2</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66">3</span><span style="color:#E06C75">})</span><span style="color:#56B6C2">-</span><span style="color:#C678DD">?</span><span style="color:#E06C75">(\d{</span><span style="color:#D19A66">7</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66">8</span><span style="color:#E06C75">})$</span><span style="color:#56B6C2">/</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">// 电子邮箱</span></span>
<span class="line"><span style="color:#56B6C2">/^</span><span style="color:#E06C75">\w</span><span style="color:#56B6C2">+</span><span style="color:#E06C75">([</span><span style="color:#56B6C2">-+</span><span style="color:#ABB2BF">.</span><span style="color:#E06C75">]\w</span><span style="color:#56B6C2">+</span><span style="color:#E06C75">)</span><span style="color:#56B6C2">*</span><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">\w+</span><span style="color:#E06C75">([</span><span style="color:#56B6C2">-</span><span style="color:#ABB2BF">.</span><span style="color:#E06C75">]\w</span><span style="color:#56B6C2">+</span><span style="color:#E06C75">)</span><span style="color:#56B6C2">*</span><span style="color:#E06C75">\</span><span style="color:#ABB2BF">.</span><span style="color:#E06C75">\w</span><span style="color:#56B6C2">+</span><span style="color:#E06C75">([</span><span style="color:#56B6C2">-</span><span style="color:#ABB2BF">.</span><span style="color:#E06C75">]\w</span><span style="color:#56B6C2">+</span><span style="color:#E06C75">)</span><span style="color:#56B6C2">*</span><span style="color:#E06C75">$</span><span style="color:#56B6C2">/</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">// 身份证号码</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">// 普通校验</span></span>
<span class="line"><span style="color:#56B6C2">/</span><span style="color:#E06C75">(</span><span style="color:#56B6C2">^</span><span style="color:#E06C75">\d{</span><span style="color:#D19A66">15</span><span style="color:#E06C75">}$)</span><span style="color:#56B6C2">|</span><span style="color:#E06C75">(</span><span style="color:#56B6C2">^</span><span style="color:#E06C75">\d{</span><span style="color:#D19A66">18</span><span style="color:#E06C75">}$)</span><span style="color:#56B6C2">|</span><span style="color:#E06C75">(</span><span style="color:#56B6C2">^</span><span style="color:#E06C75">\d{</span><span style="color:#D19A66">17</span><span style="color:#E06C75">}(\d</span><span style="color:#56B6C2">|</span><span style="color:#E06C75">X</span><span style="color:#56B6C2">|</span><span style="color:#E06C75">x)$)</span><span style="color:#56B6C2">/</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">// 精准校验 18位</span></span>
<span class="line"><span style="color:#56B6C2">/^</span><span style="color:#E06C75">[</span><span style="color:#D19A66">1</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">]\d{</span><span style="color:#D19A66">5</span><span style="color:#E06C75">}(</span><span style="color:#D19A66">19</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">20</span><span style="color:#E06C75">)\d{</span><span style="color:#D19A66">2</span><span style="color:#E06C75">}((</span><span style="color:#D19A66">0</span><span style="color:#E06C75">[</span><span style="color:#D19A66">1</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">])</span><span style="color:#56B6C2">|</span><span style="color:#E06C75">(</span><span style="color:#D19A66">1</span><span style="color:#E06C75">[</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">2</span><span style="color:#E06C75">]))(([</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">2</span><span style="color:#E06C75">][</span><span style="color:#D19A66">1</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">])</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">10</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">20</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">30</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">31</span><span style="color:#E06C75">)\d{</span><span style="color:#D19A66">3</span><span style="color:#E06C75">}[</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#E06C75">9Xx]$</span><span style="color:#56B6C2">/</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">// 15位</span></span>
<span class="line"><span style="color:#56B6C2">/^</span><span style="color:#E06C75">[</span><span style="color:#D19A66">1</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">]\d{</span><span style="color:#D19A66">5</span><span style="color:#E06C75">}\d{</span><span style="color:#D19A66">2</span><span style="color:#E06C75">}((</span><span style="color:#D19A66">0</span><span style="color:#E06C75">[</span><span style="color:#D19A66">1</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">])</span><span style="color:#56B6C2">|</span><span style="color:#E06C75">(</span><span style="color:#D19A66">1</span><span style="color:#E06C75">[</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">2</span><span style="color:#E06C75">]))(([</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">2</span><span style="color:#E06C75">][</span><span style="color:#D19A66">1</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">])</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">10</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">20</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">30</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">31</span><span style="color:#E06C75">)\d{</span><span style="color:#D19A66">2</span><span style="color:#E06C75">}[</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#E06C75">9Xx]$</span><span style="color:#56B6C2">/</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">// 后6位</span></span>
<span class="line"><span style="color:#56B6C2">/^</span><span style="color:#E06C75">(([</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">2</span><span style="color:#E06C75">][</span><span style="color:#D19A66">1</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">])</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">10</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">20</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">30</span><span style="color:#56B6C2">|</span><span style="color:#D19A66">31</span><span style="color:#E06C75">)\d{</span><span style="color:#D19A66">3</span><span style="color:#E06C75">}[</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#E06C75">9Xx]$</span><span style="color:#56B6C2">/</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">// QQ号码</span></span>
<span class="line"><span style="color:#56B6C2">/^</span><span style="color:#E06C75">[</span><span style="color:#D19A66">1</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">][</span><span style="color:#D19A66">0</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">]\d{</span><span style="color:#D19A66">4</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66">9</span><span style="color:#E06C75">}$</span><span style="color:#56B6C2">/</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">// 邮政编码</span></span>
<span class="line"><span style="color:#56B6C2">/^</span><span style="color:#E06C75">[</span><span style="color:#D19A66">1</span><span style="color:#56B6C2">-</span><span style="color:#D19A66">9</span><span style="color:#E06C75">]\d{</span><span style="color:#D19A66">5</span><span style="color:#E06C75">}$</span><span style="color:#56B6C2">/</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">// 注册账号</span></span>
<span class="line"><span style="color:#56B6C2">/^</span><span style="color:#E06C75">[a</span><span style="color:#56B6C2">-</span><span style="color:#E06C75">zA</span><span style="color:#56B6C2">-</span><span style="color:#E06C75">Z][a</span><span style="color:#56B6C2">-</span><span style="color:#E06C75">zA</span><span style="color:#56B6C2">-</span><span style="color:#E06C75">Z0</span><span style="color:#56B6C2">-</span><span style="color:#E06C75">9_]{</span><span style="color:#D19A66">4</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66">15</span><span style="color:#E06C75">}$</span><span style="color:#56B6C2">/</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div><div class="collapsed-lines"></div></div></div></template>


